#! /usr/bin/env lua
-- sync *.txt <--> farming.*.tr files
-- very basic sync helper
-- does not support multiline entries
-- 20220225 by SwissalpS

-- language code
local sL = 'pt'
local sFile1 = sL .. '.txt'
-- may need to change mod name here
local sFile2 = 'farming.' .. sL .. '.tr'
-- filenames of output
local sFileOut1 = 'suggested.' .. sFile1
local sFileOut2 = 'suggested.' .. sFile2

-------------------------------------------------------
-------------------------------------------------------

local function readFile(sFile)
	local iLine = 0
	local sKey, sPhrase, iPos
	local a = { lines = {}, phrases = {} }
	local handle = io.open(sFile, 'r')
	for sLine in handle:lines() do
		iLine = iLine + 1
		table.insert(a.lines, sLine)
		if '' ~= sLine and '#' ~= sLine:sub(1,1) then
			-- let's just hope no phrases contain escaped '='s
			-- and none are multiline
			iPos = sLine:find('=')
			if iPos then
				sKey = sLine:sub(1, iPos - 1)
				sPhrase = sLine:sub(iPos + 1)
				a.phrases[sKey] = sPhrase
			else
				print('found line without "=" #' .. iLine .. ': ' .. sLine)
			end
		end
	end
	handle:close()
	return a
end -- readFile


local function writeFile(sFile, a)
	local aLines = {}
	local sKey, sPhrase, iPos
	for iLine, sLine in ipairs(a.lines) do
		if '' ~= sLine and '#' ~= sLine:sub(1,1) then
			iPos = sLine:find('=')
			if iPos then
				sKey = sLine:sub(1, iPos - 1)
				sPhrase = a.phrases[sKey]
				sLine = sKey .. '=' .. sPhrase
			end
		end
		table.insert(aLines, sLine)
	end
	local sOut = table.concat(aLines, '\n')
	local handle = io.open(sFile, 'w')
	handle:write(sOut)
	handle:close()
	print('wrote to file ' .. sFile)
end -- writeFile


function getChoice(sKey, s1, s2)
	local sIn
	while true do
		print(sKey)
		print('[1]' .. s1 .. '\n[2]' .. s2 .. '\n[3] let me type an improved version')
		sIn = io.stdin:read()
		if '1' == sIn then return s1 end
		if '2' == sIn then return s2 end
		if '3' == sIn then return io.stdin:read() end
	end
end -- getChoice

-------------------------------------------------------
-------------------------------------------------------

-- read files
local aFile1 = readFile(sFile1)
local aFile2 = readFile(sFile2)

-- compare files
local aConflicts = {}
local sPhrase2
local iConflicts = 0
for sKey, sPhrase1 in pairs(aFile1.phrases) do
	sPhrase2 = aFile2.phrases[sKey]
	if not sPhrase1 or '' == sPhrase1 then
		-- not set, let's check if other file has it
		if sPhrase2 and '' ~= sPhrase2 then
			-- has something, let's set ours to it
			aFile1.phrases[sKey] = sPhrase2
		end
	elseif sPhrase2 ~= sPhrase1 then
		-- other file has something else
		if not sPhrase2 or '' == sPhrase2 then
			-- set other to ours
			aFile2.phrases[sKey] = sPhrase1
		else
			-- both have something but it's not the same
			aConflicts[sKey] = { sPhrase1, sPhrase2 }
			iConflicts = iConflicts + 1
		end
	end
end

if 0 < iConflicts then
	-- resolve conflicts
	local sRes
	print('have ' .. iConflicts .. ' conflicts:')
	for sKey, aOptions in pairs(aConflicts) do
		sRes = getChoice(sKey, aOptions[1], aOptions[2])
		aFile1.phrases[sKey] = sRes
		aFile2.phrases[sKey] = sRes
	end
else
	print('OK, no conflicts.')
end

-- output suggested changes
writeFile(sFileOut1, aFile1)
writeFile(sFileOut2, aFile2)

